package com.tale.ssm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.util.StringUtil;
import com.tale.ssm.entity.User;
import com.tale.ssm.service.UserService;
import com.tale.ssm.vo.Page;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(ModelAndView model) {
		model.setViewName("user/user_list");
		
		return model;
	}
	
	
	@RequestMapping(value = "/get_list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getList(
			@RequestParam(value="userName",required=false,defaultValue="")String userName,
			Page page
			){
		System.out.println("page:" + page.toString());
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("userName", "%"+userName+"%");
		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());
		map.put("rows", userService.findList(queryMap));
		map.put("total", userService.getTotal(queryMap));
		return map;
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public Map<String, String> delete(
			@RequestParam(value="ids[]")Long[] ids,
			HttpServletRequest request
			){
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "error");
		if(ids.length == 0) {
			map.put("msg", "请选择删除用户");
			return map;
		}
		User user = (User) request.getSession().getAttribute("user");
		String id = "";
		for(Long i:ids) {
			if(i == user.getId()) {
				map.put("msg", "对不起，不能删除自己");
				return map;
			}
			id += i + ",";
		}
		id = id.substring(0, id.length() - 1);
		if(userService.delete(id) <= 0) {
			map.put("msg", "删除失败");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "删除成功");
		
		return map;
	}
	
	@PostMapping("/edit")
	@ResponseBody
	public Map<String, String> edit(User user){
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "error");
		if(StringUtil.isEmpty(user.getUserName())) {
			map.put("msg", "用户名不能为空");
			return map;
		}
		if(StringUtil.isEmpty(user.getPassword())) {
			map.put("msg", "密码不能为空");
			return map;
		}
		boolean falg = userService.findByUserName(user.getUserName()) != null;
		if(falg) {
			map.put("msg", "已经存在该用户名");
			return map;
		}
		
		if(userService.edit(user) <= 0) {
			map.put("msg", "修改失败");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "修改成功");
		
		return map;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> add(User user) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "error");
		if(user == null) {
			map.put("msg", "");
			return map;
		}
		if(user.getUserName().equals("") || user.getUserName().isEmpty()) {
			map.put("msg", "用户名不能为空");
			return map;
		}
		if(user.getPassword().equals("") || user.getPassword().isEmpty()) {
			map.put("msg", "密码不能为空");
			return map;
		}
		boolean flag = userService.findByUserName(user.getUserName()) != null;
		if(flag) {
			map.put("msg", "用户已经存在");
			return map;
		}
		if(userService.add(user) <= 0) {
			map.put("msg", "添加失败");
			return map;
		}
//		userService.add(user);
		map.put("type", "success");
		map.put("msg", "添加成功");
		
		return map;
	}
}
